package patientimport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.followup.plat.entity.BsEvaluationStandard;
import com.bbcare.followup.plat.store.ibatis.IFollowEvaluationDAO;

@ContextConfiguration(locations = { "classpath*:/ibatis-config.xml",
		"classpath*:/spring-mvc.xml","classpath*:/spring-mybatis.xml"
		})

//AIMS曲线数据导入数据库
public class ImportAIMSStandards extends AbstractTestNGSpringContextTests{
	@Autowired
	private IFollowEvaluationDAO followEvaluationDao;
	
	@Test
	public void test() throws Exception{
		importPercentStandard();
	}
	
	private void importPercentStandard() throws FileNotFoundException,
			IOException, Exception {
		File files = new File("F:\\bbcare\\交接文档\\资料\\ALberta运动量表\\百分位");
		File[] fs = files.listFiles();
//		FollowEvaluationService service = (FollowEvaluationService) applicationContext.getBean("followEvaluationService");

		for(File f : fs){
			System.out.println("---"+f.getAbsolutePath());

			List<BsEvaluationStandard> list = new ArrayList<BsEvaluationStandard>();
			String filePath = f.getAbsolutePath();
//			String[] names = filePath.substring(filePath.lastIndexOf("\\")+1).split("-");
//			if(names.length == 2) {
				FileReader reader = new FileReader(filePath);
		        BufferedReader br = new BufferedReader(reader);
		        String str = br.readLine();
//		        String[] titles = str.split("	");   //FamilyReportService
    	        while((str = br.readLine()) != null) {
	        		String [] values = str.split("	");
	        		BsEvaluationStandard data = new BsEvaluationStandard();
	        		data.setAge(Float.parseFloat(values[8]));
	        		data.setAgeMax(Integer.parseInt(values[1]));
	        		data.setAgeMin(Integer.parseInt(values[0]));
	        		data.setCreateAuthor("admin");
	        		data.setCreateTime(DateUtil.getSysDateA());
	        		data.setId(SeqUtil.getSeqNo());
	        		data.setP5(Double.parseDouble(values[2]));
	        		data.setP10(Double.parseDouble(values[3]));
	        		data.setP25(Double.parseDouble(values[4]));
	        		data.setP50(Double.parseDouble(values[5]));
	        		data.setP75(Double.parseDouble(values[6]));
	        		data.setP90(Double.parseDouble(values[7]));
	        		data.setStandardId(Constants.AIMS_STANDARD_ID);
	        		data.setStandardType(Constants.AIMS_STANDARD_TYPE_PERCENT);
	        		data.setProjectId(Constants.AIMS_DEVELOPPROJECT_SCORE);
	        		data.setProjectName("AIMS百分位数据");
	        		list.add(data);
    	        }
    	        followEvaluationDao.saveBsEvaluationStandard(list);
    	        br.close();
    		    reader.close();
			}
		}
//	}
}
